import rest from '@/utils/rest'
import type { FormInstance } from 'element-plus'
import useRouteUtil from '@/hooks/useRouteUtil'
import { gp } from '@gp'

type Options = {
  res: string
  action: string
  meta: boolean // 开启元数据获取
}

export default function (options: Options) {
  const res = options.res
  const formRef = ref<FormInstance>()
  const action = options.action
  const meta = options.meta == undefined ? false : options.meta

  const { goBack, refreshParent } = useRouteUtil()
  const route = useRoute()
  const params =
    route.query.params == undefined
      ? {}
      : JSON.parse(decodeURIComponent(<string>route.query.params))
  const data = reactive({
    title: '',
    loading: false,
    disabled: false,
    form: {},
    meta: {},
    labelPosition: '',
    rules: {},
    formAction: 'add',
    params: params,
  })

  onMounted(() => {
    const id = data.params.id
    if (action == undefined) {
      if (id == undefined) {
        data.formAction = 'add'
        data.title = '新增'
      } else {
        data.formAction = 'edit'
        data.title = '编辑'
        loadFormData()
      }
    } else if (action === 'add') {
      data.title = '新增'
      data.formAction = 'add'
    } else if (action === 'edit') {
      data.formAction = 'edit'
      data.title = '编辑'
      loadFormData()
    }

    // 获取元数据
    if (meta) {
      loadMeta()
    }
  })

  const loadFormData = async () => {
    const id = data.params.id
    if (id == undefined) {
      gp.$baseMessage('页面参数id为空', 'error', 'vab-hey-message-error')
      return false
    }
    data.loading = true
    data.disabled = true
    const { data: row } = await rest.get(res, id).finally(() => {
      data.loading = false
      data.disabled = false
    })
    data.form = row
  }

  const loadMeta = async () => {
    const { data: meta } = await rest.meta(res)
    data.meta = meta
  }

  const submitForm = () => {
    // @ts-ignore
    formRef.value.validate(async (valid) => {
      if (valid) {
        data.disabled = true
        data.loading = true
        if (data.formAction == 'add') {
          // 发送新增 http请求
          const { code } = await rest.create(res, data.form).finally(() => {
            data.disabled = false
            data.loading = false
          })
          if (code == 200) {
            gp.$baseMessage('新增成功', 'success', 'vab-hey-message-success')
            refreshParent()
            goBack()
          }
        } else if (data.formAction == 'edit') {
          // 发送更新 http请求
          const id = data.params.id
          const { code } = await rest.update(res, id, data.form).finally(() => {
            data.disabled = false
            data.loading = false
          })
          if (code == 200) {
            gp.$baseMessage('编辑成功', 'success', 'vab-hey-message-success')
            refreshParent()
            goBack()
          }
        }
      }
    })
  }

  const resetForm = () => {
    // @ts-ignore
    formRef.value.resetFields()
  }

  return {
    formRef,
    data,
    submitForm,
    resetForm,
    goBack,
  }
}
